$(function () {
var vm = new Vue({
            el: "#content",
            data: {
               meetings:[],
               meetingapplys:[],
               managemeetings:[],
               managemeetingsall:[]
            },
			methods:{
				//添加会议室
				addmeeting:function(){
		
					var mm_name=$("#mm_name").val();
					var mm_script=$("#mm_script").val();
					$.ajax({
					url:"../addmeeting",
					data:{mm_name:mm_name,mm_script:mm_script},
					type:"post",
					dataType:"json",
					success:function(result){
						if(result!=0){
							alert("添加成功")
							location.reload();
						}
					}
				})
				},
				//修改会议室模态框触发事件
				showupdatemodel:function(){
					var el = event.currentTarget;
					$("#updates").modal("show");
	      	   		var mm_id=$(el).parent().siblings("td[name='mmid']").find("input[type='hidden']").val();
	      			var mm_name=$(el).parent().siblings("td[name='mmname']").html();
	      			var mm_script=$(el).parent().siblings("td[name='mmscript']").html();
	      			$("#upmm_id").val(mm_id);
	      			$("#upmm_name").val(mm_name);
	      			$("#upmm_script").val(mm_script);
	      			console.log("mmid="+mm_id+"mmname="+mm_name+"mmscript="+mm_script)	
				},
				//审核会议室模态框触发事件
				showmeetingcheckmodel:function(){
					var el = event.currentTarget;
					$("#meetingcheck").modal("show");
					var ma_id=$(el).parent().siblings("td[name='ma_id']").find("input[type='checkbox']").val();
	      	   		var ma_name=$(el).parent().siblings("td[name='ma_name']").html();
	      			var ma_starttime=$(el).parent().siblings("td[name='ma_starttime']").html();
	      			var ma_stoptime=$(el).parent().siblings("td[name='ma_stoptime']").html();
	      			var ma_stype=$(el).parent().siblings("td[name='ma_stype']").html();
	      			var ma_status=$(el).parent().siblings("td[name='ma_status']").html();
	      			var ma_username=$(el).parent().siblings("td[name='ma_username']").html();
	      			var ma_applytime=$(el).parent().siblings("td[name='ma_applytime']").html();
	      			$("#cma_id").val(ma_id);
	      			$("#cma_username").html(ma_name);
	      			$("#cma_status").html(ma_status);
	      			$("#cma_name").html(ma_name);
	      			$("#cma_starttime").html(ma_starttime);
	      			$("#cma_stoptime").html(ma_stoptime);
	      			$("#cma_stype").html(ma_stype);
	      			$("#cma_applytime").html(ma_applytime);
	      			console.log("ma_id="+ma_id+"ma_starttime="+ma_starttime+"ma_stoptime="+ma_stoptime)	
				},
				//审核会议室
				checkmeeting:function(){
					var ma_id=$("#cma_id").val();
					var ma_status=$("#meetingcheck").find("input[name='checkmeeting']:checked").val();
					console.log("ma_id="+ma_id+"ma_status="+ma_status)	
					$.ajax({
					url:"../checkmeeting",
					data:{ma_id:ma_id,ma_status:ma_status},
					type:"post",
					dataType:"json",
					success:function(result){
						if(result!=0){
							alert("操作成功")
							location.reload();
						}
					}
				})
				},
				//修改会议室
				updatemeeting:function(){
					var mm_id=$("#upmm_id").val();
					var mm_name=$("#upmm_name").val();
					var mm_script=$("#upmm_script").val();
					console.log("mmid="+mm_id+"mmname="+mm_name+"mmscript="+mm_script)	
					$.ajax({
					url:"../updatemeeting",
					data:{mm_id:mm_id,mm_name:mm_name,mm_script:mm_script},
					type:"post",
					dataType:"json",
					success:function(result){
						if(result!=0){
							alert("修改成功")
							location.reload();
						}
					}
				})
				},
				//删除会议室
				delmeeting:function(){
					var el = event.currentTarget;
					var mm_ids=$(el).parent().siblings("td[name='mmid']").find("input[type='hidden']").val()+",";
					console.log("mmids="+mm_ids)	
					$.ajax({
					url:"../delmeeting",
					data:{mm_ids:mm_ids},
					type:"post",
					dataType:"json",
					success:function(result){
						if(result!=0){
							alert("删除成功")
							location.reload();
						}
					}
				})
				},
				//批量删除会议室
				delmeetings:function(){
					var el = event.currentTarget;
					var mmids=""
					$(el).parent().find("input[name='selmeeting']:checked").each(function (index, item){
						mmids=mmids+$(item).val() + ","
					})
						console.log(mmids)
					if(mmids==""){
						alert("请选择要取消预定的会议室哟");
						return;
					}else{
					
						$.ajax({
							url:"../delmeeting",
							data:{mm_ids:mmids},
							type:"post",
							dataType:"json",
							success:function(result){
								console.log(result);
								if(result!=0){
									alert("删除成功")
									location.reload();
								}
							}
						})
					}
				},
			//首次加载或者按条件查询会议室
			Search:function(curr){
				var _self = this;
				var starttime=$("#starttime").val();
				var stoptime=$("#stoptime").val();
				$.ajax({
				url:"../querymeetingtime",
				data:{ma_starttime:starttime,ma_stoptime:stoptime,curr:curr,pagesize:3},
				type:"post",
				dataType:"json",
				success:function(result){
					_self.meetings=result.mlist
					//分页插件
				       layui.use(['laypage', 'layer'], function(){
				    	   var laypage = layui.laypage
				    	   ,layer = layui.layer;
				    	   
				    	   //总页数低于页码总数
				    	   laypage.render({
				    	     elem: 'meetingtimepages'
				    	     ,count: result.totle //数据总数
				    	     ,limit:3
				    	    ,curr:curr
				    	   , layout: ['count', 'prev', 'page', 'next', 'skip']
				    	     ,jump: function(obj, first){
				    	         if(!first){
				    	        	 vm.Search(obj.curr)
				    	         }
				    	       }
				    	   })
				       });
				}
			})
			},
			//查询所有会议室
			Searchmeeting:function(curr){
				var _self = this;
				$.ajax({
				url:"../querymeeting",
				data:{curr:curr,pagesize:2},
				type:"post",
				dataType:"json",
				success:function(result){
					console.log(result)
					_self.managemeetings=result.mlist
					//分页插件
				       layui.use(['laypage', 'layer'], function(){
				    	   var laypage = layui.laypage
				    	   ,layer = layui.layer;
				    	   
				    	   //总页数低于页码总数
				    	   laypage.render({
				    	     elem: 'meetingpages'
				    	     ,count: result.totle //数据总数
				    	     ,limit:2
				    	    ,curr:curr
				    	   , layout: ['count', 'prev', 'page', 'next', 'skip']
				    	     ,jump: function(obj, first){
				    	         if(!first){
				    	        	 vm.Searchmeeting(obj.curr)
				    	         }
				    	       }
				    	   })
				       });
				}
			})
			},
			//按条件查询申请的会议室
			ConditionSearch:function(curr){
				var _self = this;
				var starttime=$("#mstarttime").val();
				var stoptime=$("#mstoptime").val();
				var ma_username=$("#mmausername").val();
				var ma_name=$("#mapplyname").val();
				var ma_stype=$("#mmeetingstype").val();
				
				$.ajax({
				url:"../querymeetingapply",
				data:{ma_starttime:starttime,ma_stoptime:stoptime,ma_username:ma_username,ma_name:ma_name,ma_stype:ma_stype,curr:curr,pagesize:2},
				type:"post",
				dataType:"json",
				success:function(result){
					console.log(result.malistall);
					_self.meetingapplys=result.malist
					_self.meetingapplysall=result.malistall
					//分页插件
				       layui.use(['laypage', 'layer'], function(){
				    	   var laypage = layui.laypage
				    	   ,layer = layui.layer;
				    	   
				    	   //总页数低于页码总数
				    	   laypage.render({
				    	     elem: 'meetingapplypages'
				    	     ,count: result.totle //数据总数
				    	     ,limit:2
				    	    ,curr:curr
				    	   , layout: ['count', 'prev', 'page', 'next', 'skip']
				    	     ,jump: function(obj, first){
				    	         if(!first){
				    	        	 vm.ConditionSearch(obj.curr)
				    	         }
				    	       }
				    	   })
				       });
					
				}
			})
			},
			//取消申请的会议室
			delApply:function(){
	
				var el = event.currentTarget;
				var maids=""
				$(el).parent().find("input[name='del']:checked").each(function (index, item){
				
					maids=maids+$(item).val() + ","
					$(item).parent().parent().remove()
					})
					console.log(maids+typeof(maids))
				if(maids==""){
					alert("请选择要取消预定的会议室哟");
					return;
				}else{
				
					$.ajax({
						url:"../delApply",
						data:{maids:maids},
						type:"post",
						dataType:"json",
						success:function(result){
							console.log(result);
							if(result==0){
								alert("取消成功");
							}
						}
					})
				}
			},
			//申请会议室
			Apply:function(){
			
				var starttime=$("#starttime").val();
				var stoptime=$("#stoptime").val();
            	//获取所选会议室名称
            	var ma_name=$("input[name='sel']:checked").parent().siblings("td:first").html();
            	//获取所选会议室id
            	var ma_mmid=$("input[name='sel']:checked").siblings("input:first").val()
            	var ma_username=$("#applyname").val();
            	var ma_stype=$("#meetingstype option:selected").val();
            	if(starttime==null||starttime==""){
            		alert("请输入使用会议室的开始时间")
            		return
            	}
            	if(starttime==null||starttime==""){
            		alert("请输入使用会议室的结束时间")
            		return
            	}
            	if(ma_name==null||ma_name==""){
            		alert("请输入选择会议室")
            		return
            	}
            	if(ma_username==null||ma_username==""){
            		alert("请输入您的名字")
            		return
            	}
            	if(ma_stype==null||ma_stype==""){
            		alert("请输入用途")
            		return
            	}else{
            		$.ajax({
        				url:"../Applymeeting",
        				data:{ma_starttime:starttime,ma_stoptime:stoptime,ma_mmid:ma_mmid,ma_name:ma_name,ma_username:ma_username,ma_stype:ma_stype},
        				type:"post",
        				dataType:"json",
        				success:function(result){
        					console.log(result)
        					if(result!=0){
        						alert("申请成功")
        						location.reload();
        					}
        				}
        			})
            	}
			vm.Search(1)
			}
			}
        });
               vm.Search(1);
               vm.Searchmeeting(1);
               vm.ConditionSearch(1);
         //新增会议室模态框触发事件    
           $("#addmeeting").click(function(){
              		$("#add").modal("show");
              	})
            });
//动态获取当前时间
       function current(){ 
    	   var d=new Date(),str=''; 
    	   str +=d.getFullYear()+'年'; //获取当前年份 
    	   str +=d.getMonth()+1+'月'; //获取当前月份（0——11） 
    	   str +=d.getDate()+'日'; 
    	   str +=d.getHours()+'时'; 
    	   str +=d.getMinutes()+'分'; 
    	   return str; 
    	   }
       //获取当前时间与申请的结束时间比较
    	   setInterval(function(){
    		   $("#nowTime").html(current)
    		 var ma=vm.meetingapplysall;
    		   for(var i=0;i<ma.length;i++){
    			   if(current()==ma[i].ma_stoptime){//如果相等就调用删除方法将该条数据删除
    				   console.log("maid"+ma[i].ma_id)
    				   $.ajax({
   						url:"../delApply",
   						data:{maids:ma[i].ma_id},
   						type:"post",
   						dataType:"json",
   						success:function(result){
   							console.log(result);
   							if(result==1){
   								alert("取消成功");
   							}
   						}
   					})
        		   }
    		   } 
    		   },60000); 
    	   
     lay('.ctime').each(function(){
       laydate.render({
    	   elem: this
    	   ,type: 'datetime'
    	   ,trigger: 'click'
    	   ,format:'yyyy年M月d日H时m分'
    	 })
       });
       layui.use('element', function(){
    	   var $ = layui.jquery
    	   ,element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
    	   
    	   //触发事件
    	   var active = {
    	   tabChange: function(){
    	       //切换到指定Tab项
    	       element.tabChange('demo', '22'); //切换到：用户管理
    	     }
    	   };
    	   $('.site-demo-active').on('click', function(){
    	     var othis = $(this), type = othis.data('type');
    	     active[type] ? active[type].call(this, othis) : '';
    	   });
    	 });
       

